﻿#region Header

/*
Remote views based on linq
By

Pascal Hauser 
Dipl. Ing. in Informatik, Hochschule für Technik Rapperswil, 2006
Master Thesis, Hochschule für Technik Rapperswil, 2008-2010

and

Raphael Gfeller
Dipl. Ing. in Informatik, Hochschule für Technik Rapperswil, 2006
Master Thesis, Hochschule für Technik Rapperswil, 2008-2010

*/

#endregion

#region Usings

using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Threading;

#endregion

namespace InterLinq.Test.UI {
	/// <summary>
	/// Interaction logic for SumByOrderPositionForm.xaml
	/// </summary>
	public partial class MultiContentWindow : Window {
		#region Public Methods

		/// <summary>
		/// creates a new window that displays n <see cref="UserControls"/>
		/// </summary>
		/// <param name="controls">to be displayed</param>
		/// <param name="title">title of window</param>
		/// <param name="closeTimeoutSeconds">time to close automatically[s]</param>
		public MultiContentWindow(int closeTimeoutSeconds, params UserControl[] controls) {
			InitializeComponent();
			foreach (UserControl ctr in controls) {
				m_root.Children.Add(ctr);
			}
			m_closeTimeoutBar.Maximum = closeTimeoutSeconds;
			m_timer = new DispatcherTimer();
			m_timer.Interval = new TimeSpan(0, 0, 1);
			m_timer.Tick += OnTick;
			PreviewMouseMove += OnPreviewMouseMove;
			m_timer.Start();
		}

		#endregion

		#region protected Methods

		protected override void OnClosing(CancelEventArgs e) {
			base.OnClosing(e);
			Cleanup();
		}

		#endregion

		#region Private Methods

		private void OnPreviewMouseMove(object sender, MouseEventArgs e) {
			m_closeTimeout = (int)m_closeTimeoutBar.Minimum;
		}

		private void OnTick(object sender, EventArgs e) {
			m_closeTimeout++;
			m_closeTimeoutBar.Value = m_closeTimeout;
			if (m_closeTimeout >= m_closeTimeoutBar.Maximum) {
				Cleanup();
				Close();
			}
		}

		private void Cleanup() {
			m_timer.Tick -= OnTick;
			m_timer.Stop();
			PreviewMouseMove -= OnPreviewMouseMove;
		}

		#endregion

		#region Private Fields

		private readonly DispatcherTimer m_timer;
		private int m_closeTimeout;

		#endregion
	}
}